package com.ligy.easypoi;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jeecgframework.poi.word.WordExportUtil;
import org.jeecgframework.poi.word.entity.WordImageEntity;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.junit.Test;

import com.ligy.easypoi.model.PayeeEntity;

public class EasyPoiTest {

	@Test
	public void test1() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("workers", "10");
		map.put("workinfdays", "10");

		try {
			XWPFDocument doc = WordExportUtil.exportWord07(
					"D:/easypoi/template.docx", map);
			FileOutputStream fos = new FileOutputStream(
					"D:/easypoi/report.docx");
			doc.write(fos);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Test
	public void test2() throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("IPhone 5s", new Double(20));
		dataset.setValue("SamSung Grand", new Double(20));
		dataset.setValue("MotoG", new Double(40));
		dataset.setValue("Nokia Lumia", new Double(10));

		JFreeChart chart = ChartFactory.createPieChart("Mobile Sales", // chart
																		// title
				dataset, // data
				true, // include legend
				true, false);

		chart.setBackgroundImageAlpha(1.0F);
//		chart.setBackgroundPaint(new Color(0, 0, 0));
		LegendTitle legend = chart.getLegend();
//		legend.setBackgroundPaint(new Color(0, 0, 0));
		
		Plot plot = chart.getPlot();
		plot.setBackgroundPaint(new Color(255, 255, 255));
		
		
		
		int width = 400; /* Width of the image */
		int height = 300; /* Height of the image */
		
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    ChartUtilities.writeChartAsJPEG(baos, chart, width, height);

		File _file = new File("D:/easypoi/testCode.png");
		BufferedImage src = ImageIO.read(_file);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ImageIO.write(src, "png", out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		WordImageEntity image = new WordImageEntity();
		image.setHeight(200);
		image.setWidth(500);
		image.setData(baos.toByteArray());
		image.setType(WordImageEntity.Data);
		map.put("testCode", image);
		
		List<PayeeEntity> payees = new ArrayList<PayeeEntity>();
		for (int i = 0; i < 10; i++) {
			payees.add(new PayeeEntity("name" + i, "bankAccount" + i, "bankName" + i));
		}
		map.put("payees", payees);
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 1; i++) {
			Map<String, Object> testMap = new HashMap<String, Object>();

			testMap.put("id", "xman");
			testMap.put("name", "小明" + i);
			testMap.put("sex", "1");
			mapList.add(testMap);
		}
		map.put("maplist", mapList);
		try {
			XWPFDocument doc = WordExportUtil.exportWord07(
					"D:/easypoi/Image.docx", map);
			FileOutputStream fos = new FileOutputStream(
					"D:/easypoi/imageReport.docx");
			doc.write(fos);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
